<%@ page language="java" pageEncoding="UTF-8"%>
<script type="text/javascript">

	var childData = {};
	
	var childrenDetailsGrid = {};
	
	var relatedField = {};
	
	function getChild(relatedData) {
		$.ajax({
			url : '${baseURL}/jdbc/common/basecommonlist/childList.do?mdId=${module.moduleId}',
			type : 'post',
			dataType : 'json',
			success : function(data) {
				if (data) {
					for(var key in data){
						var module = data[key].module;
						childData[module.moduleId] = data[key];
						var searchParam = [];
						if (relatedData) {
							for(var d in relatedData){
								if (relatedData[d].fieldId == data[key].relatedField) {
									relatedField[data[key].fieldName] = relatedData[d].value;
									searchParam.push({'name':data[key].fieldName, 'value':relatedData[d].value, 'type':'exact-match'});
								}
							}
						}
						

						var str = JSON.parse(module.selectorData || '[]');
						if(str && str.length>0){
							initData($(".selector-form",$("#"+module.moduleId+"formsearch")),str, [], module.module);	
							var searchCondition = JSON.parse(module.searchCondition || '[]');
							for (var i = 0; i < searchCondition.length; i++) {
								var each = searchCondition[i];
								if(each.data){
									setSearchContainer(each.id, each.name, each.data);
								}else if(each.url){
								  	$.ajax({
										type: "POST",
										async : false,
										url: baseURL + "/" + each.url,
										success: function(data){
											if(data){
												setSearchContainer(each.id, each.name, data);
											}
										}
								 	});
								}
							}
							//渲染选择器
						  	renderSelector(getDefaultSelector());
							var gridColumns = genColumns(module.moduleDisplay);
							var url = '${baseURL}/jdbc/common/basecommonlist/listJSON.do?mdId=' + module.moduleId + '&defaultSearchParam=' + JSON.stringify(searchParam);
							var detailsGrid = $("#"+module.moduleId).ligerGrid({
								columns : gridColumns,
								usePager : true,
						        toolbar:{},allowHideColumn:false,
						        width: '100%', height: '400',heightDiff:-20, checkbox: true,
						        rowHeight:23,rownumbers:true,isScroll:false,pageSizeOptions:[5,10,20],pagesizeParmName:"perPageSize",
						        pageSize: 5
							});
							
							if(detailsGrid){
								//加载toolbar
							    LG.createToolbarButton(detailsGrid, [{id : module.moduleId+"_add", name : "新增", clickEvent : FF_AddClickEvent, icon : "a fa fa-plus-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_modify", name : "修改", clickEvent : FF_ModifyClickEvent, icon : "a fa fa-pencil-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_delete", name : "删除", clickEvent : FF_DeletedClickEvent, icon : "a fa fa-minus-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_import", name : "导入", clickEvent : FF_ImportClickEvent, icon : "a fa fa-cloud-upload" ,type:"icon"},
							    								{id : module.moduleId+"_export", name : "导出", clickEvent : FF_ExportClickEvent, icon : "a fa fa-cloud-download" ,type:"icon"}]);
								
								//双击事件
							   	LG.setGridDoubleClick(detailsGrid, FF_ModifyClickEvent);
								
							   	childrenDetailsGrid[module.moduleId] = detailsGrid;
							}
							
							initSearchGrid(detailsGrid,url);
						}else{
							var gridColumns = genColumns(module.moduleDisplay);
							var url = '${baseURL}/jdbc/common/basecommonlist/listJSON.do?mdId=' + module.moduleId + '&defaultSearchParam=' + JSON.stringify(searchParam);
							var detailsGrid = $("#"+module.moduleId).ligerGrid({
								columns : gridColumns,
								usePager : true,url:url,
						        toolbar:{},allowHideColumn:false,
						        width: '100%', height: '400',heightDiff:-20, checkbox: true,
						        rowHeight:23,rownumbers:true,isScroll:false,pageSizeOptions:[5,10,20],pagesizeParmName:"perPageSize",
						        pageSize: 5
							});
							
							if(detailsGrid){
								//加载toolbar
							    LG.createToolbarButton(detailsGrid, [{id : module.moduleId+"_add", name : "新增", clickEvent : FF_AddClickEvent, icon : "a fa fa-plus-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_modify", name : "修改", clickEvent : FF_ModifyClickEvent, icon : "a fa fa-pencil-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_delete", name : "删除", clickEvent : FF_DeletedClickEvent, icon : "a fa fa-minus-square-o" ,type:"icon"},
							    								{id : module.moduleId+"_import", name : "导入", clickEvent : FF_ImportClickEvent, icon : "a fa fa-cloud-upload" ,type:"icon"},
							    								{id : module.moduleId+"_export", name : "导出", clickEvent : FF_ExportClickEvent, icon : "a fa fa-cloud-download" ,type:"icon"}]);
								
								//双击事件
							   	LG.setGridDoubleClick(detailsGrid, FF_ModifyClickEvent);
								
							   	childrenDetailsGrid[module.moduleId] = detailsGrid;
							}
						}
					}
				}
			}
		});
	}
	
	
	function genColumns(data) {
		if (data) {
			data = JSON.parse(data);
			var columns = new Array();
			for ( var key in data) {
				if (!data[key].isHidden) {
					columns.push({
						display : data[key].display,
						name : data[key].name,
						width : '120',
						type : data[key].type,
						align : "center",
						isSort : true
					});
				}
			}
			return columns;
		}
		return null;
	}

	//工具条事件
	//新增事件
	function FF_AddClickEvent(btnItem) {
		if(eventObj && eventObj.beforeContorlChild){
			if(!eventObj.beforeContorlChild()){
				return false;
			}
		}
		var moduleId = btnItem.id.replace("_add","");
		FF_ShowDialog(null, true, moduleId);
	}

	//修改事件
	function FF_ModifyClickEvent(btnItem) {
		if(eventObj && eventObj.beforeContorlChild){
			if(!eventObj.beforeContorlChild()){
				return false;
			}
		}
		var moduleId = btnItem.id.replace("_modify","");
		var row = childrenDetailsGrid[moduleId].getLastSelected();
		if (!row) {
			win.$.ligerDialog.warn("请先选择需要修改的数据!");
			return false;
		}
		FF_ShowDialog(row, false, moduleId);
	}

	//删除事件
	function FF_DeletedClickEvent(btnItem) {
		if(eventObj && eventObj.beforeContorlChild){
			if(!eventObj.beforeContorlChild()){
				return false;
			}
		}
		var moduleId = btnItem.id.replace("_delete","");
		var selectedRow = childrenDetailsGrid[moduleId].getCheckedRows();
		if (!selectedRow || !selectedRow.length) {
			win.$.ligerDialog.warn("请先选择需要删除的数据!");
			return false;
		}

		jQuery.ligerDialog.confirm('您确定删除吗?', function(confirm) {
			if (confirm) {
				$.ajax({
					type : "GET",
					url : "${baseURL}/jdbc/common/basecommondelete/delete.do?mdId="+moduleId,
					dataType : "json",
					data : "ids=" + LG.serialIdParam(selectedRow),
					beforeSend : function() {
						LG.loading = true;
						LG.showLoading('正在处理中...请稍候');
					},
					complete : function() {
						LG.loading = false;
						LG.hideLoading();
					},
					success : function(msg) {
						if (msg && msg.hasOk) {
							childrenDetailsGrid[moduleId].loadData();
							win.$.ligerDialog.success('操作成功!');
						} else {
							win.$.ligerDialog.error("操作失败!失败信息如下:\n" + msg.message
									+ "\n如您对以上信息有疑问，请联系管理人员!");
						}
					}
				});
			}
		});
	}
	
	//导入事件
    function FF_ImportClickEvent(btnItem){
    	if(eventObj && eventObj.beforeContorlChild){
			if(!eventObj.beforeContorlChild()){
				return false;
			}
		}
    }
    
    //导出事件
 	function FF_ExportClickEvent(btnItem){
 		if(eventObj && eventObj.beforeContorlChild){
			if(!eventObj.beforeContorlChild()){
				return false;
			}
		}
 		var moduleId = btnItem.id.replace("_export","");
 		var data = getSelectorValue();
 		window.location.href = "${baseURL}/jdbc/common/basecommonlist/export.do?mdId="+moduleId+"&searchParam="+JSON.stringify(data);
    }
</script>